package com.it.glide.glidesample;

import android.util.Log;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class WebServiceUtils {

	
	public static int timeOut=36000;

	/**
	 * 同步方法 仅供imageloader
	 * 
	 */
	public static void callSyncWebService(String url, final String namespace, final String methodName,
			HashMap<String, String> properties, final WebServiceCallBack webServiceCallBack) {
		final HttpTransportSE httpTransportSE = new HttpTransportSE(url, timeOut);

		SoapObject soapObject = new SoapObject(namespace, methodName);

		if (properties != null) {
			for (Iterator<Map.Entry<String, String>> it = properties.entrySet().iterator(); it.hasNext();) {
				Map.Entry<String, String> entry = it.next();
				soapObject.addProperty(entry.getKey(), entry.getValue());
			}
		}

		final SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.setOutputSoapObject(soapObject);
		soapEnvelope.bodyOut = soapObject;
		soapEnvelope.dotNet = false;


		SoapObject resultSoapObject = null;
		try {
			httpTransportSE.call(null, soapEnvelope);
			if (soapEnvelope.getResponse() != null) {

				resultSoapObject = (SoapObject) soapEnvelope.bodyIn;
				SoapObject ob = resultSoapObject;
				String jsonStr = ob.getProperty(0).toString();
				webServiceCallBack.onSuccess(jsonStr);
//				mHandler.sendMessage(mHandler.obtainMessage(1, resultSoapObject));

			} else {
				throw new Exception("结果为null !");
			}
		} catch (Exception e) {
			e.printStackTrace();
//			mHandler.sendMessage(mHandler.obtainMessage(0, e));
			webServiceCallBack.onFailure(e.toString());
		}
	}

	
	

	
	
	
	
	
	
	
	
	private final String IMAGE_TYPE = "image/*";

	/**
	 * 图片上传方法
	 * 
	 * 1.把图片信息通过Base64转换成字符串 2.调用connectWebService方法实现上传
	 */
	private void uploadImg(String filePath) {
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(filePath);
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int count = 0;
			while ((count = fis.read(buffer)) >= 0) {
				baos.write(buffer, 0, count);
			}
			String uploadBuffer = ""; // = new
										// String(Base64.encode(baos.toByteArray()));
										// //进行Base64编码
			connectWebService(uploadBuffer);
			Log.i("connectWebService", "start");
			fis.close();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	
	
	/**
	 * 通过webservice实现图片上传
	 * 
	 * @param imageBuffer
	 */
	private void connectWebService(String imageBuffer) {
		// 以下就是 调用过程了，不明白的话 请看相关webservice文档
		SoapObject soapObject = new SoapObject("namespace", "methodName");
		soapObject.addProperty("filename", "my.jpg"); // 参数1 图片名
		soapObject.addProperty("image", imageBuffer); // 参数2 图片字符串
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		envelope.dotNet = false;
		envelope.setOutputSoapObject(soapObject);
		HttpTransportSE httpTranstation = new HttpTransportSE("url");
		try {
			httpTranstation.call("", envelope);
			Object result = envelope.getResponse();
			Log.i("connectWebService", result.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	

}
